import { nextTick } from './nextTick'

const queue = []
let isRunning = false

function flushSchduleQueue() {
  while (queue.length) {
    const watcher = queue.shift()
    watcher.run()
  }
  isRunning = false
}

export function queueWatcher(watcher) {
  if (!queue.includes(watcher)) {
    queue.push(watcher)
  }
  if (!isRunning) {
    isRunning = true
    nextTick(flushSchduleQueue)
  }
}
